package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.result.Result;
import com.itheima.mp.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能：
 * 作者：4204001140
 * 日期：2024/5/9 19:07
 */

@RestController
@RequestMapping("/users")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 新增用户
     * @return
     */
    @PostMapping
    @ApiOperation("新增用户")
    public Result add(@RequestBody UserFormDTO userFormDTO){
        User user = new User();
        BeanUtils.copyProperties(userFormDTO,user);
        userService.save(user);
        return Result.success();
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public Result delete(@PathVariable Long id){
        userService.removeById(id);
        return Result.success();
    }

    /**
     * 根据id查询用户 + 地址
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询用户")
    public Result<UserVO> getById(@PathVariable Long id){
        return Result.success(userService.getUserAndAddressById(id));
    }

    /**
     * 根据id批量查询用户
     * @param ids
     * @return
     */
    @GetMapping
    @ApiOperation("根据id批量查询用户")
    public Result<List<UserVO>> getByIds(@RequestParam List<Long> ids){
        return Result.success(userService.getUserAndAddressByIds(ids));
    }

    /**
     * 根据id扣减余额
     * @param id
     * @param money
     * @return
     */
    @PutMapping("/{id}/deduction/{money}")
    @ApiOperation("根据id扣减余额")
    public Result deduction(@PathVariable Long id, @PathVariable Integer money){
        userService.deduction(id,money);
        return Result.success();
    }

    /**
     * 条件查询
     * @param userQuery
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("条件查询")
    public Result<List<UserVO>> list(UserQuery userQuery){
        /*

        // 法一：
        // 1.创建wrapper对象，使用lambda方法构建条件构造器
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda()
                .like(userQuery.getName() != null, User::getUsername,userQuery.getName())
                .eq(userQuery.getStatus() != null, User::getStatus,userQuery.getStatus())
                .ge(userQuery.getMinBalance() != null, User::getBalance,userQuery.getMinBalance())
                .le(userQuery.getMaxBalance() != null, User::getBalance,userQuery.getMaxBalance());

        // 2.调用service方法
        List<User> userList = userService.list(wrapper);
        List<UserVO> userVOList = BeanUtil.copyToList(userList, UserVO.class);
        return Result.success(userVOList);

        */

        // 法二：
        return Result.success(BeanUtil.copyToList(userService.lambdaQuery()
                .like(userQuery.getName() != null, User::getUsername, userQuery.getName())
                .eq(userQuery.getStatus() != null, User::getStatus, userQuery.getStatus())
                .ge(userQuery.getMinBalance() != null, User::getBalance, userQuery.getMinBalance())
                .le(userQuery.getMaxBalance() != null, User::getBalance, userQuery.getMaxBalance())
                .list(), UserVO.class));
    }

    /**
     * 分页查询
     * @param userQuery
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageDTO<UserVO>> pageQuery(UserQuery userQuery){
        return Result.success(userService.pageQuery(userQuery));
    }

    /**
     * // 批量新增
     * @param userFormDTOList
     * @return
     */
    @PostMapping("/batch")
    @ApiOperation("批量新增")
    public Result addBatch(List<UserFormDTO> userFormDTOList) {
        userService.saveBatch(BeanUtil.copyToList(userFormDTOList, User.class));
        return Result.success();
    }

}
